package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.PickingEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class MaterialTransferActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.etLocation)
    FreeUI_ClearEditText etLocation;
    @BindView(R.id.tvMaterialDesc)
    TextView tvMaterialDesc;
    @BindView(R.id.tvMaterialType)
    TextView tvMaterialType;
    @BindView(R.id.tvEquipmentId)
    TextView tvEquipmentId;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materialtransfer;
    }

    @Override
    protected void initView() {
        tvTitle.setText("物料调拨");
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {
        initEdit(etLot, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(),getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(MaterialTransferActivity.this, false, getSupportFragmentManager())) {
                        tvMaterialDesc.setText(response.body().getResponse().getBody().getMLOTLIST().get(0).getMATERIALDESC());
                        tvMaterialType.setText(response.body().getResponse().getBody().getMLOTLIST().get(0).getMATERIALTYPE());
                        tvEquipmentId.setText(response.body().getResponse().getBody().getMLOTLIST().get(0).getUDF().getEquipmentId());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "MLOTALLOCATION");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "QUERY");
            map.addProperty("MLOTID", s);
            NetUtils.requestPostNet("IF.MLOTALLOCATION", "Common", MaterialTransferActivity.this, map, callBack);

        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                        (new TypeToken<BaseResponseBean<Object>>() {
                        }.getType(),getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<Object>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(MaterialTransferActivity.this, false, getSupportFragmentManager())) {
                            showToast("物料调拨成功",true);
                            etLot.setText("");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<Object>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "MLOTALLOCATION");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "CHANGE");
                map.addProperty("MLOTID", getText(etLot));
                map.addProperty("EQUIPMENTID", getText(etEqpID));
                map.addProperty("LOCATION", getText(etLocation));
                NetUtils.requestPostNet("IF.MLOTALLOCATION", "Common", MaterialTransferActivity.this, map, callBack);
                break;
        }
    }

}